<?php

/*
 * Site login functions
 */

function mct_sl_postpage($url, $cookie){
            //NOW GET THE PAGE
        $ch = curl_init();
        // SET FILE TO DOWNLOAD
        $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_USERAGENT, $agent);
        curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
        curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        // EXECUTE REQUEST (FILE DOWNLOAD)
        $content = curl_exec ($ch);
        //check results
        $info = curl_getinfo($ch);
        if ($info['http_code'] != 200) {
            //log error
            mct_ai_log('Savelink',MCT_AI_LOG_ERROR, 'Curl error: '.$info['http_code'],$url);
            if (empty($info)){
                return '';
            }
        }
        // CLOSE CURL
        curl_close ($ch); 
        return diffbot_post($content, $url);

}

function abs_url($url, $pageContent){
    $linkhost = parse_url($url, PHP_URL_HOST);
    $linkscheme = parse_url($url, PHP_URL_SCHEME);
    $pageContent = preg_replace('{href="/}','href="'.$linkscheme.'://'.$linkhost.'/',$pageContent);
    $pageContent = preg_replace("{href='/}","href='".$linkscheme.'://'.$linkhost.'/',$pageContent);
    $pageContent = preg_replace('{src="/}','src="'.$linkscheme.'://'.$linkhost.'/',$pageContent);
    $pageContent = preg_replace("{src='/}","src='".$linkscheme.'://'.$linkhost.'/',$pageContent);
    return $pageContent;
}

//Diff Bot page grab/format
function diffbot($url){
    $dbot = 'http://www.diffbot.com/api/article?token=3b695edfbba1bf45c58c6b7e949355ea&url='.$url.'&html=true';
    $itm = json_decode(file_get_contents($dbot));
    return diffbot_page($itm);
}

function diffbot_page($itm){
    $article = '';
    if ($itm->{'html'} == '') {
        if ($itm->{'text'} != '') {
            $article = $itm->{'text'};
            $article = preg_replace('/(\r?\n|\r)/', '</p><p>', $article);
	    $article = '<p>' . str_replace('<p></p>', '', $article) . '</p>';
        }
        else {
            if ($itm->{'media'} == ''){
                return '';  //page not rendered into text or media
            }
        }
    }
    else {
        $article = $itm->{'html'};
    }

    $images = '';
    if ($itm->{'media'} != '') {
        $images = '<div id="box_media">';
        foreach ($itm->{'media'} as $media){
            if (stripos($article,$media->{'link'}) !== false){
                continue;  //dup so skip it
            }
            if (stripos($images,$media->{'link'}) !== false){
                continue;  //dup so skip it
            }
            if (stripos($media->{'link'},'wsj.net') !== false){
                //Duplicate images with _D_ and _G_ values
                if (stripos($media->{'link'},'_G_') !== false){
                    continue;
                }
            }
            if ($media->{'type'} == 'video'){
                $images .= '<iframe title="Video Player" class="youtube-player" type="text/html" ';
                $images .= 'width="250" height="250" src="'.$media->{'link'}.'"';
                $images .= 'frameborder="0" allowFullScreen></iframe>';
            }
            else {
                $images .= '<img id="side_image" src="'.$media->{'link'}.'">';
            }
        }
        $images .= '</div>';
    }
    $pageContent = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
    $pageContent .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">';
    $pageContent .= '<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
    $pageContent .= '<link rel="stylesheet" type="text/css" href="'.plugins_url().'/SaveLink/savelink.css" />';
    //Check if no text and set up for a redirect
    $txt_chk = wp_strip_all_tags($article);
    $txt_chk = preg_replace('/\s\s+/', '',$txt_chk);  //remove all white space, even spaces between words
    if(strlen($txt_chk) < 10){
        $pageContent .= '<!--Redirect{'.$itm->{'url'}.'}-->';
    }
    $pageContent .= '<title>'.$itm->{'title'}.'</title></head>';
    $pageContent .= '<body><div id="savelink-article">';
    $pageContent .= '<h1 id="sl-title">'.$itm->{'title'}.'</h1>';
    if ($itm->{'author'}){
        $pageContent .= '<div>Author: '.$itm->{'author'}.'</div>';
    }
    $hoststr = parse_url($itm->{'url'},PHP_URL_HOST);
    $pageContent .= '<div id="source-url"><a href="'.$itm->{'url'}.'" >Click here to view original web page at '.$hoststr.'</a></div>';
    $pageContent .= '<div>';
    if ($images != ''){
        $pageContent .= $images;
    }
    $pageContent .= '<span>'.$article.'</span>';
    $pageContent .= '</div></body></html>';
    $a = 'b';
    return $pageContent;
}

function diffbot_post($content,$url){
    // INIT CURL
    $ch = curl_init();
    // SET URL FOR THE POST FORM LOGIN
    curl_setopt($ch, CURLOPT_URL, 'http://www.diffbot.com/api/article?token=3b695edfbba1bf45c58c6b7e949355ea&url='.$url.'&html=true');
    // ENABLE HTTP POST
    curl_setopt ($ch, CURLOPT_POST, 1);
    // Don't worry about certificate
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    // set header
    curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-type: text/html'));//,'Content-Length: ' . strlen($a)));
    // SET POST FIELD to the content
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $content);
    //Force curl to return results and not display
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    // EXECUTE REQUEST
    $store = curl_exec ($ch);
    curl_close($ch);
    return diffbot_page(json_decode($store));
}
        /*ViewText.org  use &rl=false to keep original links
        $pageContent = file_get_contents('http://viewtext.org/api/text?url='.$url.'&rl=false');
        $pageContent = preg_replace('{href="/}','href="http://viewtext.org/',$pageContent);
         * 
         */
        /*fivefilters grab
        $feed = fetch_feed("http://fivefilters.org/content-only/makefulltextfeed.php?url=".$url);
        $itm = $feed->get_item(0);
        $pageContent = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
        $pageContent .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">';
        $pageContent .= '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
        $pageContent .= '<head><title>'.$itm->get_title().'</title></head>';
        $pageContent .= "<body><h1>".$itm->get_title()."</h1>".$itm->get_description().'</body></html>';
        $a = 'b';
        */        
?>
